<?php

/*
 * Author: TruongNghia
 * Email: nghiatruong@vietnambiz.com
 * Date:27/04/2011
 */

class cate {

    function cate() {
        
    }

    function get_all_articles($id) {
        global $db, $config;

        $arrCatId = get_cat_id_to_arr($id);
        $str_cat_id = '';
        foreach ($arrCatId as $key => $arrValue) {
            if ($str_cat_id == '') {
                $str_cat_id .= $arrCatId[$key]['id'];
            } else {
                $str_cat_id .= $arrCatId[$key]['id'] . ',';
            }
        }
        if ($str_cat_id == '') {
            $sql = "SELECT count(id) as numrows FROM article  WHERE cat_id = " . $id . " AND status='yes' ORDER BY create_date DESC";
        } else {
            $str_cat_id = $str_cat_id .','. $id;
            $sql = "SELECT count(id) as numrows FROM article  WHERE cat_id = " . $id . " OR cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC";
        }

        $rsTotalRows = $db->query_first($sql);
        $totalRows = $rsTotalRows['numrows'];
        $totalPages = ceil($totalRows / $config ['total_article_on_category']);
        $start = (($config ['page'] * $config ['total_article_on_category']) - $config ['total_article_on_category']);
        $limit = $config ['total_article_on_category'];
        
        if ($str_cat_id == '') {
            $sql2 = "SELECT id,cat_id,link,title,image,short_description,view_count,description,create_date FROM article  WHERE cat_id=" . $id . " AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
        } else {
            $sql2 = "SELECT id,cat_id,link,title,image,short_description,view_count,description,create_date FROM article  WHERE cat_id=" . $id . " OR cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
        }

        $rsArticles = $db->query($sql2);
        return array(
            "totalRows" => $totalRows,
            "totalPages" => $totalPages,
            "rsArticles" => $rsArticles
        );
    }

    function get_cate_name($link) {
        global $db;
        $sqlGetCatId = "SELECT id,title,cat_id FROM article WHERE link='$link'";

        $rs = $db->query_first($sqlGetCatId);
        if ($rs['cat_id'] != 0) {
            $cat_cap2 = $this->get_cate_by_id($rs['cat_id']);

            if ($cat_cap2['parent_id'] != 0) {
                $cat_cap1 = $this->get_cate_by_id($cat_cap2['parent_id']);
                if ($cat_cap1['parent_id'] != 0) {
                    $cat_cap0 = $this->get_cate_by_id($cat_cap1['parent_id']);
                    return array('note' => $cat_cap0['link'], 'child' => $cat_cap1['link']);
                } else {
                    return array('note' => $cat_cap1['link'], 'child' => $cat_cap2['link']);
                }
            } else {
                return array('note' => $cat_cap2['link'], 'child' => '');
            }
        } else {
            $cat_cap1 = $this->get_cate_by_id($rs['cat_id']);
            return array('note' => $cat_cap1['link'], 'child' => '');
        }
    }

    function get_cat_id_to_arr2($parent_id = 0) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT * FROM article_category WHERE  parent_id = " . intval($parent_id));
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
            }
        }
        return $trees;
    }

    function get_cat_id_to_arr3($parent_id = 0) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT * FROM article_category WHERE  parent_id = " . intval($parent_id) . " AND cat_active=1");
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs;
            }
        }
        return $trees;
    }

    function getArticles($id, $start = '', $limit = '') {
        global $db, $config;
        $sqlGetCatId = "SELECT id,name,link FROM article_category WHERE id='$id'";
        $rsGetCatId = $db->query_first($sqlGetCatId);
        if (intval($rsGetCatId['id']) > 0) {
            $arrCatId = $this->get_cat_id_to_arr($rsGetCatId['id']);
            $str_cat_id = '';
            for ($i = 0; $i < count($arrCatId); $i++) {
                if ($i == (count($arrCatId) - 1)) {
                    $str_cat_id .= $arrCatId[$i];
                } else {
                    $str_cat_id .= $arrCatId[$i] . ',';
                }
            }

            if ($str_cat_id == '') {
                $str_cat_id = $rsGetCatId['id'];
                $sql = "SELECT count(id) as numrows FROM article WHERE cat_id = " . $rsGetCatId['id'] . " AND status='yes' ORDER BY create_date DESC";
            } else {
                $str_cat_id = $str_cat_id . ',' . $rsGetCatId['id'];
                $sql = "SELECT count(id) as numrows FROM article WHERE cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC";
            }
            $rsTotalRows = $db->query_first($sql);
            $totalRows = $rsTotalRows['numrows'];
            $totalPages = ceil($totalRows / $config ['total_article_on_category']);
            if ($start == '') {
                $start = (($config ['page'] * $config ['total_article_on_category']) - $config ['total_article_on_category']);
            }
            if ($limit == '') {
                $limit = $config ['total_article_on_category'];
            }

            if ($str_cat_id == '') {
                $sql = "SELECT id,cat_id,link,title,image,short_description,description,view_count,create_date FROM article WHERE cat_id=" . $rsGetCatId['id'] . " AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
            } else {
                $sql = "SELECT id,cat_id,link,title,image,short_description,description,view_count,create_date FROM article WHERE cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
            }

            $rsArticles = $db->query($sql);
            return array(
                "str_cat_id" => $str_cat_id,
                "cat_name" => $rsGetCatId['name'],
                "cat_link" => $rsGetCatId['link'],
                "totalRows" => $totalRows,
                "rsArticles" => $rsArticles
            );
        } else {
            header("Location: " . $config['domain']);
        }
    }

    function get_all_article_by_cat($id, $start = '0', $limit = '1') {
        global $db, $config;

        $arrCatId = $this->get_cat_id_to_arr($id);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }

        if ($str_cat_id == '') {
            $sql = "SELECT id,cat_id,link,title,image,short_description,description,create_date
                FROM article WHERE cat_id=" . $id . " AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
        } else {
            $sql = "SELECT id,cat_id,link,title,image,short_description,description,create_date
                FROM article WHERE cat_id IN ($str_cat_id) AND status='yes' ORDER BY create_date DESC LIMIT $start,$limit";
        }

        $rsArticles = $db->query($sql);
        return array('rsArticles' => $rsArticles);
    }

    function get_cate_by_title($link) {
        global $db;

        $sqlGetCatId = "SELECT id,name,link,parent_id,cat_active FROM article_category WHERE link='$link'";
        $rsGetCatId = $db->query_first($sqlGetCatId);
        return $rsGetCatId;
    }

    function get_cate_by_id($id = '', $page ='') {
        global $db;
        if ($page == '') {
            $sqlGetCatId = "SELECT id,name,link,parent_id,cat_active FROM article_category WHERE id=$id AND cat_typical=1 AND cat_active=1";
        } else {
            $sqlGetCatId = "SELECT id,name,link,parent_id,cat_active FROM article_category WHERE id=$id AND cat_active=1";
        }
        $rsGetCatId = $db->query_first($sqlGetCatId);
        return $rsGetCatId;
    }

    function get_cat_id_capduoi($parent_id, $trees=null) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT id FROM article_category WHERE  parent_id = " . intval($parent_id) . ' AND cat_active = 1 ORDER BY cat_order ASC');
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
            }
        }
        if($trees==null){
            $trees[] = $parent_id;
        }
        return $trees;
    }

    function get_cat_id_to_arr($parent_id = 0, $trees=null) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT id FROM article_category WHERE  parent_id = " . intval($parent_id) . ' AND cat_active = 1');
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
                $trees = $this->get_cat_id_to_arr($rs['id'], $trees);
            }
        }
        return $trees;
    }

    function get_cat_id_to_arr_home($parent_id = 0, $trees=null) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT id FROM article_category WHERE  parent_id = " . intval($parent_id) . ' AND cat_typical = 1 AND cat_active = 1');
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
                $trees = $this->get_cat_id_to_arr_home($rs['id'], $trees);
            }
        }
        return $trees;
    }

    function ortherArticleInCategory($create_date, $strCategoryId, $limit=10) {
        global $db, $config;
        $sql = "SELECT title,link,create_date FROM article WHERE create_date < '$create_date' AND cat_id IN ($strCategoryId) AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rs = $db->query($sql);
        return $rs;
    }

    function checkAttachFiles($id = '') {
        global $db, $config;
        $rsAttachFiles = $db->query_first("SELECT id,real_filename,filename FROM article_attach WHERE article_id=" . $id);
        return $rsAttachFiles;
    }

}

?>
